// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

/**
 * Created by eran on 14-12-21.
 */
package gui.component.mapEditor.grid
{
    import flash.display.Shape;

    public class GridLayer
    {
        private var mGridShape:Shape;

        private var mXLineNum:int;
        private var mYLineNum:int;

        public function GridLayer()
        {
        }

        //=================================//
        //=== Initialize & Dispose ========//
        //=================================//

        public function initialize(_xLineNum:int, _yLineNum:int):void
        {
            mGridShape = new Shape();
            mXLineNum = _xLineNum;
            mYLineNum = _yLineNum;
            doRefresh();
        }

        //=================================//
        //====== Public Function ==========//
        //=================================//

        public function refresh(_xLineNum:int, _yLineNum:int):void
        {
            mXLineNum = _xLineNum;
            mYLineNum = _yLineNum;
            doRefresh();
        }

        public function get container():Shape {return mGridShape;}

        //=================================//
        //====== Private Function =========//
        //=================================//

        private function doRefresh():void
        {
            mGridShape.graphics.clear();
            mGridShape.graphics.lineStyle(1, MapEditorConfig.BLOCK_GRID_COLOR);

            //==画横线
            for (var yIndex:int = 0; yIndex <= mYLineNum; yIndex++)
            {
                mGridShape.graphics.moveTo(0, yIndex * MapEditorConfig.EACH_BLOCK_SIZE);
                mGridShape.graphics.lineTo(mXLineNum * MapEditorConfig.EACH_BLOCK_SIZE,
                                           yIndex * MapEditorConfig.EACH_BLOCK_SIZE);
            }

            //==画竖线
            for (var xIndex:int = 0; xIndex <= mXLineNum; xIndex++)
            {
                mGridShape.graphics.moveTo(xIndex * MapEditorConfig.EACH_BLOCK_SIZE, 0);
                mGridShape.graphics.lineTo(xIndex * MapEditorConfig.EACH_BLOCK_SIZE,
                                           mYLineNum * MapEditorConfig.EACH_BLOCK_SIZE);
            }
        }

    }
}
